//
//  MK_LivingRoomVC.m
//  miku直播
//
//  Created by 杨尚达 on 2017/7/29.
//  Copyright © 2017年 杨尚达. All rights reserved.
//

#import "MK_LivingRoomVC.h"
#import "MK_SmallPlayer.h"
#import "MK_LivingRoomViewModel.h"
#import "MK_TitleContentVC.h"
#import "MK_BarrageShowCell.h"
#import "MK_BarrageManager.h"

#define MK_LivingRoomVC_BarrageCellName @"MK_LivingRoomVC_BarrageCellName"

#define MK_LivingRoomVC_NormalCellName @"MK_LivingRoomVC_NormalCellName"


@interface MK_LivingRoomVC ()<UICollectionViewDelegate,UICollectionViewDataSource,CAAnimationDelegate>

///直播间模型
@property(nonatomic,strong)MK_LivingRoomModel* model;

///直播间播放器控制器
@property(nonatomic,weak)MK_SmallPlayer* playeVC;

///直播间下方交流界面
@property(nonatomic,strong)MK_TitleContentVC* contentVC;



///直播间的ViewModel
VIEWMODEL_PROPERTY(MK_LivingRoomViewModel)

@end

@implementation MK_LivingRoomVC

+(instancetype)MK_LivingRoomWithRoomModel:(MK_LivingRoomModel*)model{
    
    MK_LivingRoomVC* roomVC = [MK_LivingRoomVC new];
    roomVC.model = model;
    [roomVC setupUI];
    return roomVC;
}

///初始化界面
-(void)setupUI{
    MK_SmallPlayer* player = [MK_SmallPlayer new];
    self.playeVC = player;
    player.superVC = self;
    [self addChildViewController:player];
    [self.view addSubview:player.view];
    [player.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.equalTo(@300);
    }];
    
    [self.vM MK_getLivingStreamWithRoomId:_model.roomID andBackBlock:^(MK_LivingStreamModel *model) {
        [player loadPlayerViewWithURLStr:model.hlsURL];
    } andFaileBlock:^(NSError *error) {
        
    }];
    
    NSArray<NSString*>* titleArr = @[@"聊天",@"主播",@"排行榜",@"贵族",@"直播"];
    _contentVC = [MK_TitleContentVC MK_TitleContentVCWithFrame:CGRectMake(0, 300, self.view.width, self.view.height-300) andTitleArr:titleArr andDelegate:self andTitleWidth:self.view.width/titleArr.count];
    
    _contentVC.cellCalssDic = @{MK_LivingRoomVC_BarrageCellName:[MK_BarrageShowCell class],MK_LivingRoomVC_NormalCellName:[UICollectionViewCell class]};
    
    [self.view addSubview:_contentVC.view];
    [[MK_BarrageManager share] logInRoomWith:_model.roomID];
    
    
    [[[UIButton new] rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id  _Nullable x) {
        //跳转方法
    }];
    
    
}
#pragma mark 代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 5;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
        {   ///弹幕界面
            MK_BarrageShowCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:MK_LivingRoomVC_BarrageCellName forIndexPath:indexPath];
            cell.model = self.model;
            return cell;
        }
            break;
        default:
        {   ///其他界面
            UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:MK_LivingRoomVC_NormalCellName forIndexPath:indexPath];
            return cell;
        }
            break;
    }
}
-(void)dealloc{
    [[MK_BarrageManager share]logoutRoom];
    [self.playeVC.hlsPlayer stop];
    [self.playeVC.hlsPlayer shutdown];
}

#pragma mark - 旋转 -
// 将要旋转时的回调
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    ///竖直
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait){
        [_playeVC.view mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.offset(0);
            make.height.equalTo(@300);
        }];
        _contentVC.view.hidden = NO;
        ///横屏
    }else{
        [_playeVC.view mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.offset(0);
        }];
        _contentVC.view.hidden = YES;
    }
    
}

///get方法ViewModel
VIEWMODEL_GET(MK_LivingRoomViewModel)

@end
